package com.sunjob.mroassets.controller;

import com.sunjob.common.domain.assets.Instance;
import com.sunjob.common.domain.assets.Part;
import com.sunjob.common.mroAnno.ControllerLog;
import com.sunjob.common.mroAnno.LogModuleType;
import com.sunjob.common.result.Result;
import com.sunjob.mroassets.server.InstanceService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@Api(tags = "实例信息处理接口")
public class InstanceController {
    @Autowired
    InstanceService instanceService;
    @ApiOperation("插入实例信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "实例对象（json格式）",name = "instance")
    })//描述入参信息
    @ApiResponses({
            @ApiResponse(code = 404,message = "无此路径"),
            @ApiResponse(code = 500,message = "服务内部错误"),
            @ApiResponse(code = 200,message = "访问成功")
    })
    @ControllerLog(value = LogModuleType.ASSETS)
    //对实例信息进行录入，传入实例对象的详细信息
    @RequestMapping("/insertInstance")
    //接收转化成json数据的实例信息
    public Result insertInstance(@RequestBody Instance instance){
        System.out.println(instance);
        try {
            //保存实例数据
            instanceService.insertInstance(instance);
            return Result.ok(null,"数据插入成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.fail("数据操作失败");
    }
    @ApiOperation("通过类型和id获取实体信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "实体id",name = "entityId"),
            @ApiImplicitParam(value = "实体类型",name = "type")
    })//描述入参信息
    @ApiResponses({
            @ApiResponse(code = 404,message = "无此路径"),
            @ApiResponse(code = 500,message = "服务内部错误"),
            @ApiResponse(code = 200,message = "访问成功")
    })
    //查询实例数据，需要传入实例或者部件的id，为了后续判断的需要，还要传入实体的类型
    @RequestMapping("getEntityByTypeAndID")
    public Result getEntityByTypeAndID(@RequestParam("entityId") Long entityId,@RequestParam("type") String type){
        Result result = new Result();
        //调用服务层查询数据，并且存入到返回对象中
        result.setData(instanceService.queryEntityByTypeAndID(entityId,type));
        //判断查询是否成功，成功则状态码为200，错误则为500
        if (result.getData()==null){
            result.setCode(500);
        }else {
            result.setCode(200);
        }
        return result;
    }
    @ApiOperation("通过id获取实例信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "实例id",name = "instanceId")
    })//描述入参信息
    @ApiResponses({
            @ApiResponse(code = 404,message = "无此路径"),
            @ApiResponse(code = 500,message = "服务内部错误"),
            @ApiResponse(code = 200,message = "访问成功")
    })
    //通过实例id查询实例数据，需要传入实例id
    @RequestMapping("/selectInstanceById")
    public Result selectInstanceById(@RequestParam("instanceId") Long instanceId){
        Result result = new Result();
        System.out.println("-----------");
        //调用服务层查询数据，并且存入到返回对象中
        result.setData(instanceService.queryEntityByTypeAndID(instanceId,"instance"));
        //判断查询是否成功，成功则状态码为200，错误则为500
        if (result.getData()==null){
            result.setCode(500);
        }else {
            result.setCode(200);
        }
        return result;
    }
    @RequestMapping("getAllInstance")
    public Result getAllInstance(){
        List<Instance> instances = instanceService.getAllInstance();
        return Result.ok(instances,"");
    }
    @RequestMapping("getAllPart")
    public Result getAllPart(){
        List<Part> parts = instanceService.getAllPart();
        return Result.ok(parts,"");
    }
    @RequestMapping("getInstanceByShapeId")
    public Result getInstanceByShapeId(@RequestParam("shapeId") Long shapeId){
        List<Instance> instances = instanceService.getInstanceByShapeId(shapeId);
        return Result.ok(instances,"");
    }
    @ApiOperation("批量存入部件信息接口")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "部件列表",name = "parts")
    })//描述入参信息
    @ApiResponses({
            @ApiResponse(code = 404,message = "无此路径"),
            @ApiResponse(code = 500,message = "服务内部错误"),
            @ApiResponse(code = 200,message = "访问成功")
    })
    @ControllerLog(value = LogModuleType.ASSETS)
    //批量存入部件信息，传入部件列表
    @RequestMapping("insertParts")
    public void insertParts(List<Part> parts){
        int flag = instanceService.insertParts(parts);
    }
}
